package com.ljj.service.impl;

import com.ljj.dataobject.UserInfo;
import com.ljj.repository.UserInfoRepository;
import com.ljj.service.IUserInfoService;
import com.ljj.utils.KeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoRepository userInfoRepository;

    @Override
    public String findUser(String username, String pwd) {
        UserInfo userInfo = userInfoRepository.findByUserName(username);
        if (userInfo == null){
            return "1";
        }
        if (!userInfo.getUserPassword().equals(pwd)){
            return "2";
        }
        return "0";
    }

    @Override
    public UserInfo save(String username, String pwd,String phone,String openid) {
        String s = KeyUtil.genUniqueKey();
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(s);
        userInfo.setUserName(username);
        userInfo.setUserPassword(pwd);
        userInfo.setUserPhone(phone);
        userInfo.setUserOpenid(openid);
        userInfoRepository.save(userInfo);
        return userInfo;
    }
}
